package com.johnny.xia.util.base;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.johnny.xia.util.http.HttpClientCommand;
import com.johnny.xia.util.http.HttpClientUtil;
import com.johnny.xia.util.http.HttpReturnCommand;

public class CurrencyUtil {
	
	static Logger logger =LoggerFactory.getLogger(CurrencyUtil.class);
	
	static int success = 0;
	static int error=0;
	
	public static void convert2(){
		String url="http://qq.ip138.com/hl.asp?from=USD&to=VND&q=1";
		
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {
			
			// 创建httpget.  
			HttpGet httpget = new HttpGet(url);
			
			
			Header[] headers = new Header[4];
			
			headers[0] = new BasicHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
			headers[1] = new BasicHeader("Accept-Encoding", "gzip, deflate, sdch");
			headers[2] = new BasicHeader("Accept-Language", "zh-CN,zh;q=0.8");
			headers[3] = new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
			
			httpget.setHeaders(headers);
			// 执行get请求.  
			CloseableHttpResponse response = httpclient.execute(httpget);
			try {
				// 获取响应实体  
				HttpEntity entity = response.getEntity();
				//System.out.println("httpCode:"+response.getStatusLine().getStatusCode());
				if (entity != null) {
					
					String data = EntityUtils.toString(entity,"GB2312");
					
					//System.out.println("Content:"+data);
					Document doc = Jsoup.parse(data);
					Elements es = doc.getElementsByClass("rate");
					if(es != null){
						Element e = es.get(0).getElementsByTag("tr").get(2);
						Element r = e.child(1);
						System.out.println(r.html());
						success++;
					}
				}
			}catch(Exception e){
				logger.error(e.getMessage());
				error++;
			} finally {
				response.close();
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			error++;
		} finally {
			// 关闭连接,释放资源  
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public static Float convert(String from, String to, int money) {
		try{
			HttpClientCommand command = new HttpClientCommand("http://qq.ip138.com/hl.asp?from="+from+"&to="+to+"&q="+money);
			HttpReturnCommand data = HttpClientUtil.get(command);
			Document doc = Jsoup.parse(data.getContent());
			Elements es = doc.getElementsByClass("rate");
			if (es != null) {
				Element e = es.get(0).getElementsByTag("tr").get(2);
				Element r = e.child(1);
				return Float.parseFloat(r.html());
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException("获取汇率失败");
		}
		
		return null;
	}

	public static void main(String[] args) {
		
//		HttpClientCommand command = new HttpClientCommand("http://qq.ip138.com/hl.asp?from=CNY&to=VND&q=1");
//		
//		HttpReturnCommand data = HttpClientUtil.get(command);
//		
//		Document doc = Jsoup.parse(data.getContent());
//		Elements es = doc.getElementsByClass("rate");
//		if(es != null){
//			Element e = es.get(0).getElementsByTag("tr").get(2);
//			Element r = e.child(1);
//			System.out.println(r.html());
//		}
		
		
//		System.out.println(convert("CNY", "VND", 1));
		
		new Timer().schedule(new TimerTask() {
			
			@Override
			public void run() {
				convert2();
				logger.info("success:"+success+",error:"+error);
			}
		}, 1000,30*1000);
	}
		
}
